package org.example.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.domain.po.ErpMaterialCategory;
import org.example.domain.po.ErpOrganization;
import org.example.domain.vo.ErpMaterialCategoryVo;
import org.example.domain.vo.ErpOrganizationVO;
import org.example.service.ErpMaterialCategoryService;
import org.example.mapper.ErpMaterialCategoryMapper;
import org.example.utils.Result;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
* @description 针对表【erp_material_category(产品类型表)】的数据库操作Service实现
*/
@Service
public class ErpMaterialCategoryServiceImpl extends ServiceImpl<ErpMaterialCategoryMapper, ErpMaterialCategory>
    implements ErpMaterialCategoryService{
//    @Override
//    public List<ErpMaterialCategory> findTree(Integer parentId) {
//        return baseMapper.findTree(parentId);
//    }

    @Override
    public List<ErpMaterialCategoryVo> selectOrganVOTree() {
        return baseMapper.selectOrganVOTree(0L);
    }

    @Override
    public List<ErpMaterialCategoryVo> queryParent(Long id, Long parentId, Long tenantId) {
        return baseMapper.queryParent(id, parentId, tenantId);
    }

    @Override
    public boolean updateOrgan(ErpMaterialCategory organ) {
        boolean update = lambdaUpdate().eq(ErpMaterialCategory::getId, organ.getId())
                .set(ErpMaterialCategory::getName, organ.getName())
                .set(ErpMaterialCategory::getSerialNo, organ.getSerialNo())
                .set(ErpMaterialCategory::getParentId, organ.getParentId())
                .set(ErpMaterialCategory::getSort, organ.getSort())
                .set(ErpMaterialCategory::getRemark, organ.getRemark())
                .set(ErpMaterialCategory::getUpdateTime, new Date())
                .update();
        return update;
    }

    @Override
    public boolean organInsert(ErpMaterialCategory organization) {
        if (BeanUtil.isEmpty(organization.getParentId())) organization.setParentId(0L);
        organization.setCreateTime(new Date());
        organization.setUpdateTime(new Date());
        return save(organization);
    }

    @Override
    public boolean removeBatchByOrgAbs(List<String> organAbs, Long tenantId) {
        LambdaUpdateWrapper<ErpMaterialCategory> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(ErpMaterialCategory::getName, organAbs)
                .eq(ErpMaterialCategory::getTenantId, tenantId);
        return remove(wrapper);
    }
    
    @Override
    public List<ErpMaterialCategoryVo> findCategoryByTenantId(Long tenantId) {
        return baseMapper.findCategoryByTenantId(tenantId, 0L);
    }
    
    @Override
    public List<Long> getIds(Long categoryId) {
        return baseMapper.getIds(categoryId);
    }
}




